<template>
    <div class="cmdcfg">
        <el-row>
            
            <exInput v-model.number="num" label="循环次数(0无限循环)" width="100" size="mini"></exInput>
            <span>执行了 {{runIdx}}次</span>
        </el-row>
    </div>
</template>


<script>

export default {
    components:{
    },
    props: {
        cmdInfo: { type: Object, default: {} },
        devInfo:{ type: Object, default: {} },
        cmdIdx : {},
    },
    data() {
        return {
            num : this.cmdInfo.num || 0,
            level : this.cmdInfo.level || 0,
            timer : null,
            runIdx: 0
        };
    },
    inject: ['doDevCmd','doNextCmd'],
     

    mounted() {
    },

    methods: {
        setByDevInfo(){
            if(this.cmdInfo.num) this.num = this.cmdInfo.num;
        },
        saveCmd(){
            this.cmdInfo.num = this.num;
        },
        initCmd(){
            this.runIdx = 0;
        },
        preCheck(){
            if(this.num===""){
                this.$message.error("请输入循环次数")
                return false;
            }
            return true;
        },
        runCmd(isOnce){
            if(!this.preCheck()){
                this.runIdx = 0;
                this.$emit("stopRun")
                return;
            }
            this.runIdx ++;
            this.$emit("stopRun")
            if(this.num>0 && this.runIdx>this.num){
                this.runIdx = 0;
                if(isOnce!==true) this.doNextCmd({finish:true});
            }else{
                this.sTimer=setTimeout(()=>{
                    this.doNextCmd({children:true});
                },100)
            }
        },
        stopCmd(){
            clearTimeout(this.sTimer);
            this.runIdx = 0;
            this.$emit("stopRun")
        },
        
    }
}
</script>

<style lang="less" scoped>
    .cmdcfg{
        display: inline-block;
    }
</style>
